<template>
  <div id="manage">
    <el-table :data="tableData" style="width: 100%" stripe>
      <el-table-column fixed prop="id" label="日期" > </el-table-column>
      <el-table-column fixed prop="username" label="用户名"> </el-table-column>
      <el-table-column fixed label="使用否禁用">
        <template slot-scope="scope">
          <span v-if="scope.row.status == 1">正常</span>
          <span v-else>停用</span>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.status == 1"
            type="text"
            size="small"
            @click="changePower(scope.row)"
          >
            禁用
          </el-button>
          <el-button
            v-if="scope.row.status != 1"
            type="text"
            size="small"
            @click="changePower(scope.row)"
          > 
            开启
          </el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { getAllAdmin,toggleAdminPower} from "@/network/index.js";

export default {
  name: "Manage",
  data() {
    return {
      tableData: [],
    };
  },
  methods:{
    open(message,type){
      this.$message({
        message,
        type
      })
    }
    ,
    changePower(v){
      let token = this.$store.getters.getToken;
      let status = v.status == 1?0:1;
      toggleAdminPower(token,v.id,status)
      .then((res) => {
          if(res.data.code>=200 && res.data.code <300){
            this.open('操作成功','success');
            v.status = v.status == 1 ? 0 : 1
          }
          else throw new Error(res);
      }).catch((err) => {
        this.open('操作失败','error');
        console.warn(err);
      });
    }
  },
  created() {
    let token = this.$store.getters.getToken;
    getAllAdmin(token)
      .then((res) => {
        if (res.data.code >= 200 && res.data.code < 300) {
          this.tableData = res.data.data;
        } else throw new Error(res.data.msg);
      })
      .catch((err) => {
        console.warn(err);
      });
  }
};
</script>

<style></style>
